package com.example.clxy.service.impl;

import com.example.clxy.dao.CourseMapper;
import com.example.clxy.entity.Course;
import com.example.clxy.result.PageResult;
import com.example.clxy.result.WebRestResult;
import com.example.clxy.result.business.CourseOneResult;
import com.example.clxy.result.business.PageResult.CoursePageResult;
import com.example.clxy.service.CourseService;
import com.example.clxy.util.UUIDUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author liuhaoqi
 * @Date 2020/8/8 10:12
 * @Version 1.0
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class CourseServiceImpl implements CourseService {

    @Autowired
    CourseMapper courseMapper;

    @Override
    public WebRestResult addCourse(Course course) {
        WebRestResult result = new WebRestResult();
        course.setCourseid(UUIDUtil.randomUUID());
        course.setCreatedTime(new Date());
        course.setUpdatedTime(new Date());
        course.setFlag("0");
        if (courseMapper.insertSelective(course) == 1) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("新增理论课程成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增理论课程失败！");
        }
        return result;
    }

    @Override
    public WebRestResult updateCourse(Course course) {
        WebRestResult result = new WebRestResult();
        course.setUpdatedTime(new Date());
        if (courseMapper.updateByPrimaryKeySelective(course) == 1) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("更新理论课程成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("更新理论课程失败！");
        }
        return result;
    }

    @Override
    public WebRestResult deleteCourse(String courseid) {
        WebRestResult result = new WebRestResult();
        if (courseMapper.deleteCourse(courseid) == 1) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("删除理论课程成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除理论课程失败！");
        }
        return result;
    }

    @Override
    public PageResult<CoursePageResult> getCourseList(int page, int pagesize, String coursename, String coursecode, String coursetype, String coursesemester) {
        PageResult<CoursePageResult> result = new PageResult<>();
        int begin = (page -1)*pagesize;
        int end = pagesize;

        List<CoursePageResult> list = courseMapper.getCourseList(coursename, coursecode, coursetype, coursesemester, begin, end, pagesize);
        if (list.size() > 0) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取理论课程列表成功");
            result.setTotal(list.get(0).getCnt());
            result.setPageCount(list.get(0).getPage());
            result.setRows(list);
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("没有找到理论课程");
        }
        return result;
    }

    @Override
    public CourseOneResult getCourseById(String courseid) {
        CourseOneResult result = new CourseOneResult();
        CourseOneResult course  =courseMapper.getCourseById(courseid);
        if (course == null) {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取单个理论课程失败");
            return result;
        } else {
            BeanUtils.copyProperties(course, result);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取单个理论课程成功");
            return result;
        }
    }
}
